<template>
  <div>
    <input placeholder="输入关键字搜索" v-model="keyWords">
    <table class="my-table">
      <thead>
        <tr>
          <th>就诊日期</th>
          <th>医生姓名</th>
          <th>诊断结果</th>
          <th>处方信息</th>
          <th>操作</th>
        </tr>
      </thead>
      <MyRender v-for="item in renderlist.length?renderlist:list" :key="item.id" :doctor="item.doctor" :results="item.results" :medicine="item.medicine" :utime="item.utime" :id="item.id"  :flag="flag" :allMsg="allMsg"></MyRender>
    </table>
 <MyAlert :flag="flag" :utime="obj.utime" :doctor="obj.doctor" :results="obj.results" :medicine="obj.medicine"></MyAlert>
  </div>
</template>
<script>
import MyRender from './components/MyRender.vue'
import MyAlert from './components/MyAlert.vue'

export default{
  components:{
  MyRender,MyAlert
  },
   data(){
    return{
     list:[{id:1,utime:'2020-12-1',doctor:'李四',results:'头疼',medicine:'感冒药',},
     {id:2,utime:'2020-1-11',doctor:'张三',results:'肚子痛',medicine:'肠胃药',},
     {id:3,utime:'2020-3-20',doctor:'王二麻子',results:'发烧',medicine:'退烧药',},
     {id:4,utime:'2020-1-11',doctor:'张三',results:'肚子痛',medicine:'肠胃药',},
     ],
     keyWords:'',
     renderlist:[],
    flag:false,
    obj:''
    }
   },
   methods:{
    
  allMsg(e){

    if(!this.flag){
      this.flag=true
    const num=e.target.dataset.id
    this.obj={...this.list.find(item=>item.id===+num)}
  }else{

    this.flag=false
    alert('先关闭')
    this.obj=''
  
  }
  }
},
   watch:{
    keyWords(newVal){
     let setTIME
     this.renderlist=[]
     if(setTIME) clearTimeout(setTIME)
     setTIME=setTimeout(()=>{
     this.renderlist=this.list.filter(item=>JSON.stringify(item).includes(newVal));
     if(!newVal) this.renderlist=[]
     },1000)
    }
   }
}
</script>

<style scoped>
  .my-table {
    border-collapse: collapse;
    width: 100%;
  }

  .my-table td, .my-table th {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
  }

  .my-table th {
    background-color: #f2f2f2;
  }
</style>
